﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;

namespace DSA.ItemScan
{
    public partial class ItemPropertiesPage : PhoneApplicationPage
    {
        string listname = "";
        string imgsource = "";

        public ItemPropertiesPage()
        {
            InitializeComponent();

            SetInputScope(txtQuantity);

            // HARD CODED VALUES
            txtName.Text = "Coca-Cola 33cl.";
            txtPrice.Text = "$0.80";
            txtWeight.Text = "33 cl.";
            image1.Source = new BitmapImage(new Uri("/res/cocacola.jpg", UriKind.Relative));
            imgsource = "/res/cocacola.jpg";
        }

        private void SetInputScope(TextBox textBoxControl)
        {
            InputScopeNameValue digitsInputScopeNameValue = InputScopeNameValue.TelephoneNumber;
            textBoxControl.InputScope = new InputScope()
            {
                Names = { new InputScopeName() { NameValue = digitsInputScopeNameValue } }
            };
        }

        private void MaskNumericInput(TextBox textBoxControl, bool allowDecimals)
        {
            string[] invalidCharacters = { "*", "#", ",", "(", ")", "x", "-", "+", " ", "@" };

            if (!allowDecimals)
            {
                invalidCharacters[invalidCharacters.Length - 1] = ".";
            }

            for (int i = 0; i < invalidCharacters.Length; i++)
            {
                textBoxControl.Text = textBoxControl.Text.Replace(invalidCharacters[i], "");
            }

            textBoxControl.SelectionStart = textBoxControl.Text.Length;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (this.NavigationContext.QueryString.ContainsKey("list"))
            {
                listname = this.NavigationContext.QueryString["list"];
            }
        }

        private void txtQuantity_KeyUp(object sender, KeyEventArgs e)
        {
            MaskNumericInput((TextBox)sender, false);
        }

        private void txtQuantity_KeyDown(object sender, KeyEventArgs e)
        {
            MaskNumericInput((TextBox)sender, false);
        }

        private void CheckButtonClick(object sender, EventArgs e)
        {
            // Navigate with information
            NavigationService.Navigate(new Uri("/Shopping List Menu/PivotMenu.xaml?name=" + this.txtName.Text +
                                                "&quant=" + this.txtQuantity.Text +
                                                "&list=" + this.listname + 
                                                "&price=" + this.txtPrice.Text +
                                                "&imgsource=" + this.imgsource +
                                                "&checked=true"
                                                ,UriKind.Relative));
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Shopping List Menu/PivotMenu.xaml?list=" + this.listname, UriKind.Relative));
        }
    }
}